﻿#include"Match.h"


/*==========================================↓ 菜单函数 ↓ ======================================*/
void menu1()
{
	printf("*************************\n");
	printf("**********主菜单*********\n");
	printf("********1.录入信息*******\n");
	printf("********2.查询信息*******\n");
	printf("********3.展示信息*******\n");
	printf("********0.退出***********\n");
	printf("*************************\n");
}

void menu2()
{
	printf("*************************\n");
	printf("********录入界面*********\n");
	printf("*****1.录入项目信息******\n");
	printf("*****2.录入学校信息******\n");
	printf("*****3.录入队伍信息******\n");
	printf("*****0.返回**************\n");
	printf("*************************\n");
}

void menu3()
{
	printf("*************************\n");
	printf("********查询界面*********\n");
	printf("*****1.查询项目信息******\n");//查询项目方法 √
	printf("*****2.查询学校信息******\n");//查询学校方法 √
	printf("*****3.查询队伍信息******\n");//查询队伍方法 √
	printf("*****0.返回**************\n");
	printf("*************************\n");
}

void menu4()
{
	printf("*************************\n");
	printf("********展示界面*********\n");
	printf("*****1.展示项目信息******\n");
	printf("*****2.展示学校信息******\n");
	printf("*****3.展示队伍信息******\n");
	printf("*****4.展示学校总排名****\n");
	printf("*****0.返回**************\n");
	printf("*************************\n");
}

//查询项目方法
void menu5()
{
	printf("*************************\n");
	printf("******查询项目界面*******\n");
	printf("*****1.编号查询**********\n");
	printf("*****2.名称查询**********\n");
	printf("*****3.查询前三名********\n");
	printf("*****0.返回**************\n");
	printf("*************************\n");
}

//查询学校方法
void menu6()
{
	printf("*************************\n");
	printf("******查询学校界面*******\n");
	printf("*****1.编号查询**********\n");
	printf("*****2.校名查询**********\n");
	printf("*****3.查询项目获奖情况**\n");
	printf("*****4.查询学校总分******\n");
	printf("*****5.查询各项比赛总分**\n");
	printf("*****0.返回**************\n");
	printf("*************************\n");
}

//查询队伍方法
void menu7()
{
	printf("*************************\n");
	printf("******查询队伍界面*******\n");
	printf("*****1.队名查询**********\n");
	printf("*****0.返回**************\n");
	printf("*************************\n");
}

/*=================================== ↓初始化及扩容函数定义↓  =========================================*/

void Expansion_p(P p)
{
	assert(p);
	P tmp = p; 
	ProInfo* cur = NULL;
	int newsize = tmp->size == 0 ? 10 * sizeof(ProInfo) : (tmp->size + ((tmp->size) >> 1)) * sizeof(ProInfo);
	cur = (ProInfo*)realloc(tmp->arr, newsize);
	if (cur != NULL)
	{
		tmp->arr = cur;
		tmp->size = newsize / sizeof(ProInfo);
		printf("扩容成功！\n");
	}
	else
	{
		perror("Expansion_p()::realloc()\n");
		return;
	}
}

void Expansion_s(S p)
{
	assert(p);
	S tmp = p;
	SchInfo* cur = NULL;
	int newsize = tmp->size == 0 ? 10 * sizeof(SchInfo) : (tmp->size + ((tmp->size) >> 1)) * sizeof(SchInfo);
	cur = (SchInfo*)realloc(tmp->arr, newsize);
	if (cur != NULL)
	{
		tmp->arr = cur;
		tmp->size = newsize / sizeof(SchInfo);
		printf("扩容成功！\n");
	}
	else
	{
		perror("Expansion_p()::realloc()\n");
		return;
	}
}

void InitTList(T p)
{
	assert(p);
	p->next = NULL;
}
void InitPList(P p)
{
	assert(p);
	P tmp = p;
	tmp->nums = tmp->size = 0;
	tmp->arr = NULL;
	Expansion_p(p);
}

void InitSList(S p)
{
	assert(p);
	S tmp = p;
	tmp->nums = tmp->size = 0;
	tmp->arr = NULL;
	Expansion_s(p);
}


void Init(P p1, S p2, T p3)
{
	assert(p1 && p2 && p3);
	//初始化
	InitTList(p3);
	InitPList(p1);
	InitSList(p2);

	setlocale(LC_ALL, "");
	system("title 赛事统计系统");
	system("mode con cols=160 lines=40");
}

void InitSchInfo(SchInfo* schInfo)
{
	assert(schInfo);
	for (int i = 0; i < 6; i++)
	{
		schInfo->_ProScore[i] = 0;
    }
	schInfo->_ProSize = 0;
	schInfo->_TeamSize = 0;
	schInfo->_TotalScore = 0;
}

void InitProInfo(ProInfo* proInfo)
{
	assert(proInfo);
	proInfo->_SchoolSize = 0;
	proInfo->_TeamSize = 0;
	for (int i = 0; i < SCHOOLSIZE_MAX; i++)
	{
		proInfo->_SchoolScore[i] = 0;
	}
	for (int i = 0; i < 4; i++)
	{
		proInfo->_PrizeNum[i] = 0;
	}
}

void InitTeamInfo(TeamInfo* teamInfo)
{
	assert(teamInfo);
	teamInfo->_StudentSize = 0;
	teamInfo->_TeacherSize = 0;
}
/*============================================↓ 进入系统↓  ===========================================*/
void Start(P p1, S p2, T p3)
{
	assert(p1 && p2 && p3);
	//初始化
	Init(p1, p2, p3);
	StartIn(p1, p2, p3);

}

void End(P p1, S p2, T p3)
{
	assert(p1 && p2 && p3);
	ProListRelease(p1);
	SchListRelease(p2);
	TeamListRelease(p3);
}
/*======================================== ↓ switch ↓ ================================================*/
void StartIn(P p1, S p2, T p3)
{
	assert(p1 && p2 && p3);
	int choice;
	do
	{
		menu1();
		printf("请选择：");
		scanf("%d", &choice);
		switch (choice)
		{
		case 1:
			system("cls");
			Enter(p1, p2, p3);//录入界面
			break;
		case 2:
			system("cls");
			Search(p1, p2, p3);//查询界面
			break;
		case 3:
			system("cls");
			Show(p1, p2, p3);//展示界面
			break;
		case 0:
			printf("退出系统！\n");
			break;
		default:
			system("cls");
			printf("输入有误，请重新输入！\n");
			break;
		}
	} while (choice);
}

void Enter(P p1, S p2, T p3)
{
	assert(p1 && p2 && p3);
	int choice;
	do
	{
		menu2();
		printf("请选择：");
		scanf("%d", &choice);
		switch (choice)
		{
		case 1:
			system("cls");
			EnterProject(p1, p2, p3);
			break;
		case 2:
			system("cls");
			EnterSchool(p1, p2, p3);
			break;
		case 3:
			system("cls");
			EnterTeam(p1, p2, p3);
			break;
		case 0:
			system("cls");
			break;
		default:
			system("cls");
			printf("输入有误，请重新输入！\n");
			break;
		}
	} while (choice);
	
}


void Search(P p1, S p2, T p3)
{
	assert(p1 && p2 && p3);
	int choice;
	do
	{
		menu3();
		printf("请选择：");
		scanf("%d", &choice);
		switch (choice)
		{
		case 1:
			system("cls");
			SearchProjectMethod(p1, p2, p3);
			break;
		case 2:
			system("cls");
			SearchSchoolMethod(p1, p2, p3);
			break;
		case 3:
			system("cls");
			SearchTeamMethod(p1, p2, p3);
			break;
		case 0:
			system("cls");
			break;
		default:
			system("cls");
			printf("输入有误，请重新输入！\n");
			break;
		}
	} while (choice);

}


void Show(P p1, S p2, T p3)
{
	assert(p1 && p2 && p3);
	int choice;
	do
	{
		menu4();
		printf("请选择：");
		scanf("%d", &choice);
		switch (choice)
		{
		case 1:
			system("cls");
			ShowProject(p1, p2, p3);
			break;
		case 2:
			system("cls");
			ShowSchool(p1, p2, p3);
			break;
		case 3:
			system("cls");
			ShowTeam(p1, p2, p3);
			break;
		case 4:
			system("cls");
			ShowSchoolTotalRanking(p1, p2, p3);
			break;
		case 0:
			system("cls");
			break;
		default:
			system("cls");
			printf("输入有误，请重新输入！\n");
			break;
		}
	} while (choice);

}


void SearchProjectMethod(P p1, S p2, T p3)
{
	int choice;
	do
	{
		menu5();
		printf("请选择：");
		scanf("%d", &choice);
		switch (choice)
		{
		case 1:
			system("cls");
			SearchProjectFlag(p1, p2, p3);
			break;
		case 2:
			system("cls");
			SearchProjectName(p1, p2, p3);
			break;
		case 3:
			system("cls");
			SearchFirstThree(p1, p2, p3);
			break;
		case 0:
			system("cls");
			break;
		default:
			system("cls");
			printf("输入有误，请重新输入！\n");
			break;
		}
	} while (choice);

}

void SearchSchoolMethod(P p1, S p2, T p3)
{
	int choice;
	do
	{
		menu6();
		printf("请选择：");
		scanf("%d", &choice);
		switch (choice)
		{
		case 1:
			system("cls");
			SearchSchoolFlag(p1, p2, p3);
			break;
		case 2:
			system("cls");
			SearchSchoolName(p1, p2, p3);
			break;
		case 3:
			system("cls");
			SearchPrize(p1, p2, p3);
			break;
		case 4:
			system("cls");
			SearchTotalScore(p1, p2, p3);
			break;
		case 5:
			system("cls");
			SearchCompetieionScore(p1, p2, p3);
			break;
		case 0:
			system("cls");
			break;
		default:
			system("cls");
			printf("输入有误，请重新输入！\n");
			break;
		}
	} while (choice);

}

void SearchTeamMethod(P p1, S p2, T p3)
{
	int choice;
	do
	{
		menu7();
		printf("请选择：");
		scanf("%d", &choice);
		switch (choice)
		{
		case 1:
			system("cls");
			SearchTeamName(p1, p2, p3);
			break;
		case 0:
			system("cls");
			break;
		default:
			system("cls");
			printf("输入有误，请重新输入！\n");
			break;
		}
	} while (choice);

}


/*====================================== ↓功能函数定义↓ ===========================================*/

//录入项目界面
void EnterProject(P p1, S p2, T p3)
{
	ProInfo newproject;
	InitProInfo(&newproject);
	char _Name[20];//项目名
	char _ProjectFlag[10];//项目编号
	printf("请输入项目名：");
	scanf("%s", _Name);
	printf("请输入项目编号：");
	scanf("%s", _ProjectFlag);
	strcpy(newproject._Name, _Name);
	strcpy(newproject._ProjectFlag, _ProjectFlag);

	//项目顺序表尾插...
	ProListInsertBack(p1, newproject);
	printf("录入项目成功！\n");
}
//录入学校界面
void EnterSchool(P p1, S p2, T p3)
{
	SchInfo newschool;
	InitSchInfo(&newschool);
	char _Name[20];//校名
	char _Flg[10];//学校编号
	printf("请输入学校名称：");
	scanf("%s", _Name);
	printf("请输入学校编号：");
	scanf("%s", _Flg);
	strcpy(newschool._Name, _Name);
	strcpy(newschool._Flg, _Flg);
	
	//学校顺序表尾插
	SchListInsertBack(p2, newschool);
	printf("录入学校成功！\n");
}
//录入队伍界面
void EnterTeam(P p1, S p2, T p3)
{
	if (!p2->nums)
	{
		printf("未录入任何学校，请先录入学校！\n");
		return;
	}
	if (!p1->nums)
	{
		printf("未录入任何项目，请先录入项目！\n");
		return;
	}

	int fschool, fproject;
	TeamInfo newteam;
	InitTeamInfo(&newteam);
	char _TeamName[20];//队伍名称
	char _SchoolName[20];//隶属学校名
	char _ProjectName[20];//参加的项目名称
	int _TeacherSize;//教师数量
	int _StudentSize;//学生数量
	int _Prize;//该队伍获得的奖项
	printf("请输入队伍名称：");
	scanf("%s", _TeamName);
	printf("请输入隶属的学校名：");
	scanf("%s", _SchoolName);
	while (!CheckisSchool(_SchoolName, p2))
	{
		printf("未找到该学校，请重新输入！\n");
		printf("请输入隶属的学校名：");
		scanf("%s", _SchoolName);
	}
	printf("请输入该队伍参加的项目名称：");
	scanf("%s", _ProjectName);
	while (!CheckisProject(_ProjectName, p1))
	{
		printf("未找到该项目，请重新输入！\n");
		printf("请输入该队伍参加的项目名称：");
		scanf("%s", _ProjectName);
	}
	printf("请输入该队伍的指导教师数量：");
	scanf("%d", &_TeacherSize);
	while (_TeacherSize != 1)
	{
		printf("指导教师数量最多为1名，请重新输入！\n");
		printf("请输入该队伍的指导教师数量：");
		scanf("%d", &_TeacherSize);
	}
	printf("请输入参赛学生数量：");
	scanf("%d", &_StudentSize);
	while (_StudentSize > 3)
	{
		printf("参赛学生数量最多为3名，请重新输入！\n");
		printf("请输入参赛学生数量：");
		scanf("%d", &_StudentSize);
	}
	printf("请输入该队伍在比赛中获得的奖项：\n");
	printf("0.特等奖  1.一等奖  2.二等奖  3.三等奖\n");
	printf("请输入：");
	scanf("%d", &_Prize);
	while (!(_Prize >= 0 && _Prize <= 3))
	{
		printf("输入选项有误，请重新输入！\n");
		printf("请输入该队伍在比赛中获得的奖项：\n");
		printf("0.特等奖  1.一等奖  2.二等奖  3.三等奖\n");
		printf("请输入：");
		scanf("%d", &_Prize);
	}
	//检查该学校的条件是否可以再创建一个队伍
	fschool = FindSchool(_SchoolName, p2);//该队伍隶属的学校在顺序表中的位置
	fproject = FindProject(_ProjectName, p1);//该队伍参加的项目在顺序表中的位置
	if (p2->arr[fschool]._TeamSize > TEAMSIZE_MAX)
	{
		printf("该学校的队伍数量已满！无法录入新队伍！\n");
		return;
	}

	if (p1->arr[fproject]._SchoolSize > SCHOOLSIZE_MAX)
	{
		printf("参加该项目的学校数量已满！无法录入新队伍！\n");
		return;
	}

	if (p1->arr[fproject]._TeamSize > TEAMNUMSIZE_MAX)
	{
		printf("参加该项目的队伍数量已满！无法录入新队伍！\n");
		return;
	}


	//对于队伍本身的操作
	newteam._Prize = _Prize;
	strcpy(newteam._TeamName, _TeamName);
	strcpy(newteam._SchoolName, _SchoolName);
	strcpy(newteam._ProjectName, _ProjectName);
	newteam._TeacherSize = _TeacherSize;
	newteam._StudentSize = _StudentSize;
	//队伍顺序表尾插
	TeamListInsertBack(p3, newteam);


	//对于隶属学校的操作

	strcpy(p2->arr[fschool]._TeamName[p2->arr[fschool]._TeamSize], _TeamName);//更新参赛队伍名
	strcpy(p2->arr[fschool]._ProName[p2->arr[fschool]._TeamSize], _ProjectName);//更新队伍参赛项目名
	p2->arr[fschool]._TeamPrize[p2->arr[fschool]._TeamSize++] = newteam._Prize;//更新队伍奖项

	//这里检查是否参加过项目...
	if (!p2->arr[fschool]._ProSize)
	{
		//如果参加项目数量为0，赋值，并且在该项目的总分项中加分
		strcpy(p2->arr[fschool]._JionProject[p2->arr[fschool]._ProSize], _ProjectName);
		p2->arr[fschool]._ProScore[p2->arr[fschool]._ProSize++] += JudgePrize(newteam._Prize);
	}
	else
	{
		int flg;
		//如果参加项目数量不为0，遍历判断是否参加过
		if ((flg = CheckisJoin(_ProjectName, p2, fschool)) != -1)
		{
			//参加过，在该项目的总分项中加分
			p2->arr[fschool]._ProScore[flg] += JudgePrize(newteam._Prize);
		}
		else
		{
			//未参加过,赋值，并且在该项目的总分项中加分
			strcpy(p2->arr[fschool]._JionProject[p2->arr[fschool]._ProSize], _ProjectName);
			p2->arr[fschool]._ProScore[p2->arr[fschool]._ProSize++] += JudgePrize(newteam._Prize);
		}
	}
	p2->arr[fschool]._TotalScore += JudgePrize(newteam._Prize);

	//对于参加项目的操作
	if (!p1->arr[fproject]._SchoolSize)
	{
		//如果该项目没有学校参加过，直接赋值学校名和该学校获得的分数
		strcpy(p1->arr[fproject]._SchoolName[p1->arr[fproject]._SchoolSize], _SchoolName);
		p1->arr[fproject]._SchoolScore[p1->arr[fproject]._SchoolSize++] += JudgePrize(_Prize);
		p1->arr[fproject]._TeamSize++;
		p1->arr[fproject]._PrizeNum[_Prize]++;
	}
	else
	{
		int flg;
		//如果该项目有学校参加过，遍历确认该学校是否参加过该项目
		if ((flg = CheckisJoinProject(_SchoolName, p1, fproject)) != -1)
		{
			//参加过
			p1->arr[fproject]._SchoolScore[flg] += JudgePrize(_Prize);
			p1->arr[fproject]._TeamSize++;
			p1->arr[fproject]._PrizeNum[_Prize]++;
		}
		else
		{
			//未参加过
			strcpy(p1->arr[fproject]._SchoolName[p1->arr[fproject]._SchoolSize], _SchoolName);
			p1->arr[fproject]._SchoolScore[p1->arr[fproject]._SchoolSize++] += JudgePrize(_Prize);
			p1->arr[fproject]._TeamSize++;
			p1->arr[fproject]._PrizeNum[_Prize]++;
		}
	}
	printf("录入队伍成功！\n");
}

int CheckisJoinProject(char name[20], P p, int fproject)
{
	assert(p);
	for (int i = 0; i < p->arr[fproject]._SchoolSize; i++)
	{
		if (!strcmp(name, p->arr[fproject]._SchoolName[i]))
		{
			return i;
		}
	}
	return -1;
}
//检查学校是否参加过该项目
int CheckisJoin(char name[20], S p, int fschool)
{
	assert(p);
	for (int i = 0; i < p->arr[fschool]._ProSize; i++)
	{
		if (!strcmp(name, p->arr[fschool]._JionProject[i]))
		{
			return i;
		}
	}
	return -1;
}
int FindSchool(char name[20], S p)
{
	assert(p);
	for (int i = 0; i < p->nums; i++)
	{
		if (!strcmp(name, p->arr[i]._Name))
		{
			return i;
		}
	}
	return -1;
}
int FindProject(char name[20], P p)
{
	assert(p);
	for (int i = 0; i < p->nums; i++)
	{
		if (!strcmp(name, p->arr[i]._Name))
		{
			return i;
		}
	}
	return -1;
}
int JudgePrize(enum Prize prize)
{
	return prize == _Grand_ ? 50 : (prize == _First_ ? 30 : (prize == _Second_ ? 10 : 5));
}

//展示项目
void ShowProject(P p1, S p2, T p3)
{
	if (!p1->nums)
	{
		printf("未录入任何项目！\n");
		return;
	}
	else
	{
		printf("已录入的项目有：\n");
		for (int i = 0; i < p1->nums; i++)
		{
			printf("项目名称：%s，项目编号：%s\n", p1->arr[i]._Name, p1->arr[i]._ProjectFlag);
		}
	}
}
//展示学校
void ShowSchool(P p1, S p2, T p3)
{
	if (!p2->nums)
	{
		printf("未录入任何学校！\n");
	}
	else
	{
		printf("已录入的学校有：\n");
		for (int i = 0; i < p2->nums; i++)
		{
			printf("学校名称：%s，学校编号：%s\n", p2->arr[i]._Name, p2->arr[i]._Flg);
		}
	}
}
//展示队伍
void ShowTeam(P p1, S p2, T p3)
{
	if (p3->next == NULL)
	{
		printf("未录入任何队伍！\n");
		return;
	}
	T cur = p3->next;
	while (cur != NULL)
	{
		printf("队名：%s，隶属学校名：%s，参加的项目名称：%s，指导教师：%d名，学生数量：%d名，所获奖项：%s\n", \
			cur->teams._TeamName, cur->teams._SchoolName, cur->teams._ProjectName, cur->teams._TeacherSize, \
			cur->teams._StudentSize, JudgePrizec(cur->teams._Prize));
		cur = cur->next;
	}
}
char* JudgePrizec(enum Prize prize)
{
	return prize == _Grand_ ? "特等奖" : (prize == _First_ ? "一等奖" : (prize == _Second_ ? "二等奖" : "三等奖"));
}
//展示学校总排名
void ShowSchoolTotalRanking(P p1, S p2, T p3)
{
	if (p2->nums == 0)
	{
		printf("未录入任何学校，暂无排名！\n");
		return;
	}
	for (int i = 0; i < p2->nums; i++)
	{
		int flg = 1;
		for (int j = 0; j < p2->nums - i - 1; j++)
		{
			if (p2->arr[j]._TotalScore < p2->arr[j + 1]._TotalScore)
			{
				SchInfo tmp = p2->arr[j];
				p2->arr[j] = p2->arr[j + 1];
				p2->arr[j + 1] = tmp;
				flg = 0;
			}
		}
		if (flg)
		{
			break;
		}
	}
	int count = -1;
	int score = 20000;
	for (int i = 0; i < p2->nums; i++)
	{
		if (p2->arr[i]._TotalScore < score)
		{
			count++;
			score = p2->arr[i]._TotalScore;
			printf("第%d名：%s学校，总分：%d\n", count + 1, p2->arr[i]._Name, p2->arr[i]._TotalScore);
		}
		else if (p2->arr[i]._TotalScore == score)
		{
			printf("第%d名：%s学校，总分：%d\n", count + 1, p2->arr[i]._Name, p2->arr[i]._TotalScore);
		}
	}
}

//项目编号查询
void SearchProjectFlag(P p1, S p2, T p3)
{
	if (p1->nums == 0)
	{
		printf("暂未录入任何项目，无法查询！\n");
		return;
	}
	char flg[10];
	printf("请输入项目编号：");
	scanf("%s", flg);
	for (int i = 0; i < p1->nums; i++)
	{
		if (!strcmp(p1->arr[i]._ProjectFlag, flg))
		{
			printf("查找成功！\n");
			printf("项目名：%s，项目编号：%s\n", p1->arr[i]._Name,flg);
			if (p1->arr[i]._SchoolSize > 0)
			{
				printf("参加该项目的学校：\n");
				for (int j = 0; j < p1->arr[i]._SchoolSize; j++)
				{
					printf("%s学校，得分：%d\n", p1->arr[i]._SchoolName[j], p1->arr[i]._SchoolScore[j]);
				}
				printf("参加学校数量：%d\n参加队伍数量：%d", p1->arr[i]._SchoolSize, p1->arr[i]._TeamSize);
				printf("\n该项目获奖情况：\n");
				printf("特等奖：%d个\n一等奖：%d个\n二等奖：%d个\n三等奖：%d个\n", \
					p1->arr[i]._PrizeNum[0], p1->arr[i]._PrizeNum[1], p1->arr[i]._PrizeNum[2]\
					, p1->arr[i]._PrizeNum[3]);
			}
			return;
		}
	}
	printf("该项目不存在！\n");
}
//项目名查询
void SearchProjectName(P p1, S p2, T p3)
{
	if (p1->nums == 0)
	{
		printf("暂未录入任何项目，无法查询！\n");
		return;
	}
	char name[20];
	printf("请输入项目名称：");
	scanf("%s", name);
	for (int i = 0; i < p1->nums; i++)
	{
		if (!strcmp(name, p1->arr[i]._Name))
		{
			printf("查找成功！\n");
			printf("项目名：%s，项目编号：%s\n", name, p1->arr[i]._ProjectFlag);
			if (p1->arr[i]._SchoolSize > 0)
			{
				printf("参加该项目的学校：\n");
				for (int j = 0; j < p1->arr[i]._SchoolSize; j++)
				{
					printf("%s学校，得分：%d\n", p1->arr[i]._SchoolName[j], p1->arr[i]._SchoolScore[j]);
				}
				printf("参加学校数量：%d\n参加队伍数量：%d", p1->arr[i]._SchoolSize, p1->arr[i]._TeamSize);
				printf("\n该项目获奖情况：\n");
				printf("特等奖：%d个\n一等奖：%d个\n二等奖：%d个\n三等奖：%d个\n", \
					p1->arr[i]._PrizeNum[0], p1->arr[i]._PrizeNum[1], p1->arr[i]._PrizeNum[2]\
					, p1->arr[i]._PrizeNum[3]);
			}
			return;
		}
	}
	printf("项目不存在！\n");
}
//查询指定项目前三名
void SearchFirstThree(P p1, S p2, T p3)
{
	if (p1->nums == 0)
	{
		printf("暂未录入任何项目，无法查询！\n");
		return;
	}
	char name[20];
	printf("请输入想要指定查找的项目名称：");
	scanf("%s", name);
	for (int i = 0; i < p1->nums; i++)
	{
		if (!strcmp(name, p1->arr[i]._Name))
		{
			for (int j = 0; j < p1->arr[i]._SchoolSize; j++)
			{
				int flg = 1;
				for (int k = 0; k < p1->arr[i]._SchoolSize - 1 - i; k++)
				{
					if (p1->arr[i]._SchoolScore[k] < p1->arr[i]._SchoolScore[k + 1])
					{
						//换成绩
						int tmp = p1->arr[i]._SchoolScore[k];
						p1->arr[i]._SchoolScore[k] = p1->arr[i]._SchoolScore[k + 1];
						p1->arr[i]._SchoolScore[k + 1] = tmp;
						//换学校名
						char names[20];
						strcpy(names, p1->arr[i]._SchoolName[k]);
						strcpy(p1->arr[i]._SchoolName[k], p1->arr[i]._SchoolName[k + 1]);
						strcpy(p1->arr[i]._SchoolName[k + 1],names);

						flg = 0;
					}
				}
				if (flg)
				{
					break;
				}
			}
			//打印前三名（并列也算）
			int count = -1;
			int score = 20000;
			for (int j = 0; j < p1->arr[i]._SchoolSize && count < 2; j++)
			{
				if (p1->arr[i]._SchoolScore[j] < score)
				{
					count++;
					score = p1->arr[i]._SchoolScore[j];
					printf("第%d名：%s学校，总得分：%d\n", count + 1, p1->arr[i]._SchoolName[j], \
						p1->arr[i]._SchoolScore[j]);
				}
				else if (p1->arr[i]._SchoolScore[j] == score)
				{
					score = p1->arr[i]._SchoolScore[j];
					printf("第%d名：%s学校，总得分：%d\n", count + 1, p1->arr[i]._SchoolName[j], \
						p1->arr[i]._SchoolScore[j]);
				}
			}
			return;
		}
	}
	printf("未找到该项目！\n");
}

//学校编号查询
void SearchSchoolFlag(P p1, S p2, T p3)
{
	if (p2->nums == 0)
	{
		printf("未录入任何学校，无法查询！\n");
		return;
	}
	char flg[10];
	printf("请输入学校编号：");
	scanf("%s", flg);
	for (int i = 0; i < p2->nums; i++)
	{
		if (!strcmp(flg, p2->arr[i]._Flg))
		{
			printf("查询成功！\n");
			printf("校名：%s\n学校编号：%s\n", p2->arr[i]._Name, p2->arr[i]._Flg);
			printf("参赛队伍数量：%d\n\n", p2->arr[i]._TeamSize);
			if (p2->arr[i]._TeamSize > 0)
			{
				printf("该学校参赛队伍：\n\n");
				for (int j = 0; j < p2->arr[i]._TeamSize; j++)
				{
					printf("队伍名：%s，参加的项目：%s，获奖情况：%s +%d分\n", p2->arr[i]._TeamName[j], \
						p2->arr[i]._ProName[j], JudgePrizec(p2->arr[i]._TeamPrize[j]), JudgePrize(p2->arr[i]._TeamPrize[j]));
				}
			}

			printf("\n\n共参加了%d个项目：\n", p2->arr[i]._ProSize);
			for (int j = 0; j < p2->arr[i]._ProSize; j++)
			{
				printf("项目名称：%s，项目总分：%d\n", p2->arr[i]._JionProject[j], p2->arr[i]._ProScore[j]);
			}

			printf("学校总分：%d\n", p2->arr[i]._TotalScore);
			return;
		}
	}
	printf("未找到该学校！\n");
}
//学校姓名查询
void SearchSchoolName(P p1, S p2, T p3)
{
	if (p2->nums == 0)
	{
		printf("未录入任何学校，无法查询！\n");
		return;
	}
	char name[20];
	printf("请输入学校名称：");
	scanf("%s", name);
	for (int i = 0; i < p2->nums; i++)
	{
		if (!strcmp(name, p2->arr[i]._Name))
		{
			printf("查询成功！\n");
			printf("校名：%s\n学校编号：%s\n", p2->arr[i]._Name, p2->arr[i]._Flg);
			printf("参赛队伍数量：%d\n\n", p2->arr[i]._TeamSize);
			if (p2->arr[i]._TeamSize > 0)
			{
				printf("该学校参赛队伍：\n\n");
				for (int j = 0; j < p2->arr[i]._TeamSize; j++)
				{
					printf("队伍名：%s，参加的项目：%s，获奖情况：%s +%d分\n", p2->arr[i]._TeamName[j], \
						p2->arr[i]._ProName[j], JudgePrizec(p2->arr[i]._TeamPrize[j]), JudgePrize(p2->arr[i]._TeamPrize[j]));
				}
			}

			printf("\n\n共参加了%d个项目：\n", p2->arr[i]._ProSize);
			for (int j = 0; j < p2->arr[i]._ProSize; j++)
			{
				printf("项目名称：%s，项目总分：%d\n", p2->arr[i]._JionProject[j], p2->arr[i]._ProScore[j]);
			}

			printf("学校总分：%d\n", p2->arr[i]._TotalScore);
			return;
		}
	}
	printf("未找到该学校！\n");
}

//查询项目获奖情况
void SearchPrize(P p1, S p2, T p3)
{
	if (p2->nums == 0)
	{
		printf("未录入任何学校，无法查询！\n");
		return;
	}
	char name[20];
	printf("请输入指定学校的名称：");
	scanf("%s", name);
	for (int i = 0; i < p2->nums; i++)
	{
		if (!strcmp(name, p2->arr[i]._Name))
		{
			printf("查询成功！\n");
			printf("校名：%s\n学校编号：%s\n", p2->arr[i]._Name, p2->arr[i]._Flg);
			if (p2->arr[i]._TeamSize > 0)
			{
				printf("该学校参赛队伍：\n\n");
				for (int j = 0; j < p2->arr[i]._TeamSize; j++)
				{
					printf("队伍名：%s，参加的项目：%s，获奖情况：%s +%d分\n", p2->arr[i]._TeamName[j], \
						p2->arr[i]._ProName[j], JudgePrizec(p2->arr[i]._TeamPrize[j]), JudgePrize(p2->arr[i]._TeamPrize[j]));
				}
			}
			return;
		}
	}
	printf("未找到该学校！\n");
}
//查询学校总分
void SearchTotalScore(P p1, S p2, T p3)
{
	if (p2->nums == 0)
	{
		printf("未录入任何学校，无法查询！\n");
		return;
	}
	char name[20];
	printf("请输入指定学校的名称：");
	scanf("%s", name);
	for (int i = 0; i < p2->nums; i++)
	{
		if (!strcmp(name, p2->arr[i]._Name))
		{
			printf("查询成功！\n");
			printf("校名：%s\n学校编号：%s\n", p2->arr[i]._Name, p2->arr[i]._Flg);
			printf("该学校的总分为：%d\n", p2->arr[i]._TotalScore);
			return;
		}
	}
	printf("未找到该学校！\n");
}
//查询各项比赛总分
void SearchCompetieionScore(P p1, S p2, T p3)
{
	if (p2->nums == 0)
	{
		printf("未录入任何学校，无法查询！\n");
		return;
	}
	char name[20];
	printf("请输入指定学校的名称：");
	scanf("%s", name);
	for (int i = 0; i < p2->nums; i++)
	{
		if (!strcmp(name, p2->arr[i]._Name))
		{
			printf("查询成功！\n");
			printf("校名：%s\n学校编号：%s\n", p2->arr[i]._Name, p2->arr[i]._Flg);

			printf("\n\n共参加了%d个项目：\n", p2->arr[i]._ProSize);
			for (int j = 0; j < p2->arr[i]._ProSize; j++)
			{
				printf("项目名称：%s，项目总分：%d\n", p2->arr[i]._JionProject[j], p2->arr[i]._ProScore[j]);
			}
			return;
		}
	}
	printf("未找到该学校！\n");
}

//队伍名字查询
void SearchTeamName(P p1, S p2, T p3)
{
	if (p3->next == NULL)
	{
		printf("未录入任何队伍，无法查询！\n");
		return;
	}
	char name[20];
	printf("请输入队伍名称：");
	scanf("%s", name);
	T cur = p3->next;
	while (cur != NULL)
	{
		if (!strcmp(cur->teams._TeamName, name))
		{
			printf("查找成功！\n");
			printf("队伍名：%s\n隶属学校名：%s\n参加项目名：%s\n指导教师：%d名\n参赛学生：%d人\n获得奖项：%s\n",\
				cur->teams._TeamName, cur->teams._SchoolName, cur->teams._ProjectName, cur->teams._TeacherSize\
				, cur->teams._StudentSize, JudgePrizec(cur->teams._Prize));
			return;
		}
		cur = cur->next;
	}
	printf("未找到该队伍！\n");
}

//检查是否存在学校
int CheckisSchool(char name[20], S p)
{
	assert(p);
	for (int i = 0; i < p->nums; i++)
	{
		if (!strcmp(name, p->arr[i]._Name))
		{
			return 1;
		}
	}
	return 0;
}
//检查是否存在项目
int CheckisProject(char name[20], P p)
{
	assert(p);
	for (int i = 0; i < p->nums; i++)
	{
		if (!strcmp(name, p->arr[i]._Name))
		{
			return 1;
		}
	}
	return 0;
}
/*==================================== ↓ 队伍链表功能函数 ↓ ==========================================*/

void TeamListInsertBack(T p, TeamInfo newteam)
{
	assert(p);

	T input = p;
	while (input->next != NULL)
	{
		input = input->next;
	}

	T newnode = (T)malloc(sizeof(TList));
	newnode->teams = newteam;
	newnode->next = NULL;
	input->next = newnode;
}

void TeamListRelease(T p)
{
	assert(p);
	if (p->next != NULL)
	{
		T cur = p->next;//指向哨兵位的下一个节点（动态开辟的内存）
		T del = cur;
		while (cur->next != NULL)
		{
			del = cur;
			cur = cur->next;
			free(del);
		}
		free(cur);
		cur = del = NULL;
	}
	else
	{
		return;
	}
}

/*===================================  ↓项目顺序表功能函数↓  ========================================*/

void ProListInsertBack(P p, ProInfo newproject)
{
	assert(p);
	if (p->nums == p->size)//判断扩容
	{
		Expansion_p(p);
	}
	if (p->nums < p->size)
	{
		p->arr[p->nums++] = newproject;
	}
}

void ProListRelease(P p)
{
	assert(p);
	free(p->arr);
}

/*===================================  ↓学校顺序表功能函数↓  ========================================*/

void SchListInsertBack(S p, SchInfo newschool)
{
	assert(p);
	if (p->nums == p->size)
	{
		Expansion_s(p);
	}
	if (p->nums < p->size)
	{
		p->arr[p->nums++] = newschool;
	}
}

void SchListRelease(S p)
{
	assert(p);
	free(p->arr);
}